package library.net.retrofit.interceptor;

import java.io.IOException;

import library.common.util.LogUtils;
import okhttp3.Interceptor;
import okhttp3.Response;

/**
 * @author Created by qiang.hou on 2017/12/22.
 * @version 1.0
 */
public class RxHttpLoggingInterceptor implements Interceptor {
    private int level = LEVEL.NONE;

    public RxHttpLoggingInterceptor() {
    }

    public RxHttpLoggingInterceptor(int level) {
        this.level = level;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        switch (level) {
            case LEVEL.BODY:
                LogUtils.i("LoggingInterceptor request tag = " + chain.request().tag());
                LogUtils.i("LoggingInterceptor request url = " + chain.request().url());
                LogUtils.i("LoggingInterceptor request body = " + chain.request().body());
                break;
            case LEVEL.HEADERS:
                LogUtils.i("LoggingInterceptor request tag = " + chain.request().tag());
                LogUtils.i("LoggingInterceptor request url = " + chain.request().url());
                LogUtils.i("LoggingInterceptor request headers = " + chain.request().headers().toString());
                break;
            case LEVEL.ALL:
                LogUtils.i("LoggingInterceptor request tag = " + chain.request().tag());
                LogUtils.i("LoggingInterceptor request url = " + chain.request().url());
                LogUtils.i("LoggingInterceptor request headers = " + chain.request().headers().toString());
                LogUtils.i("LoggingInterceptor request body = " + chain.request().body());
                break;
            case LEVEL.NONE:
            default:
                break;
        }
        Response response = chain.proceed(chain.request());
        switch (level) {
            case LEVEL.BODY:
                LogUtils.i("LoggingInterceptor request tag = " + chain.request().tag());
                LogUtils.i("LoggingInterceptor request url = " + chain.request().url());
                LogUtils.i("LoggingInterceptor response body = " + response.body());
                break;
            case LEVEL.ALL:
                LogUtils.i("LoggingInterceptor request tag = " + chain.request().tag());
                LogUtils.i("LoggingInterceptor request url = " + chain.request().url());
                LogUtils.i("LoggingInterceptor response headers = " + response.headers().toString());
                LogUtils.i("LoggingInterceptor response body = " + response.body());
                break;
            case LEVEL.NONE:
            default:
                break;
        }
        return response;
    }

    public static class LEVEL {
        public final static int BODY = 0;
        public final static int HEADERS = 1;
        public final static int ALL = 2;
        public final static int NONE = 3;
    }
}
